//
//  WBHomeViewController.swift
//  simpleWeibo


import UIKit

private let cellID = "cellID"
/// 原创微博Cell 重用ID
private let originalCellID = "originalCellID"
/// 转发微博Cell 重用ID
private let retweetedCellID = "retweetedCellID"
/// 转发微博Cell 重用ID
private let forwardCellID = "forwardCellID"

class WBHomeViewController: WBBaseController {
    
    lazy var statusList = [String]()
    
    fileprivate lazy var listViewModel = WBStatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @objc fileprivate func didClickLeftBarButtonItem() {
        let vc = WBDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
    override func loadData() {
        print("开始加载数据")
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            print("加载数据结束 \(String(describing: self.listViewModel.status.last?.status.text))")
            self.isPullup = false
            self.refresh?.endRefreshing()
            //刷新表格
            if shouldRefresh {
                self.tableView?.reloadData()
            }
        }
    }
}

// MARK: - 设置界面
extension WBHomeViewController {
    
    //注册原型cell
    override func setupTableView() {
        super.setupTableView()
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellID)
        tableView?.register(UINib(nibName: "WBStatusForwardlCell", bundle: nil), forCellReuseIdentifier: forwardCellID)
        //取消预估行高
//        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        tableView?.separatorStyle = .none
        setupNavTitle()
        setupLeftBarButtonItem()
    }
    
    private func setupLeftBarButtonItem() {
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(didClickLeftBarButtonItem))
    }
    
    private func setupNavTitle() {
        let title = WBNetWorkManager.shared.userAccount.screen_name
        let button = WBTitleButton(title: title)
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        navItem.titleView = button
    }
    
    @objc func clickTitleButton(button: UIButton) {
        button.isSelected = !button.isSelected
    }
}

// MARK: - 表格数据源方法
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.status.count;
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let viewModel = listViewModel.status[indexPath.row]
        
        let cellID = (viewModel.status.retweeted_status != nil) ? forwardCellID : originalCellID
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBStatusCell
        
        cell.viewModel = viewModel
        cell.delegate = self
        
        return cell
    }
    
    // 在swift2 中 没有override 没区别
    // 在swfit3 中 没有override 父类没有提供该方法 不会调用该方法 必须父类实现该方法 子类来override该方法
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let viewModel = listViewModel.status[indexPath.row]
        return viewModel.rowHeight ?? 0
        
    }
    
}

extension WBHomeViewController: WBStatusCellDelegate {
    func statusCellDidSelectedURLString(cell: WBStatusCell, urlString: String) {
        print("\(urlString)")
        print("\(cell)")
        
        let vc = WBWebViewController()
        vc.urlString = urlString
        navigationController?.pushViewController(vc, animated: true)
    }
}
